//
//  PeoplePickerExampleViewController.m
//  PeoplePickerExample
//

#import "PeoplePickerExampleViewController.h"

@implementation PeoplePickerExampleViewController

- (void)updateNameLabelForPerson:(ABRecordRef)person {
	CFStringRef name = ABRecordCopyCompositeName(person);
	labelContactName.text = [NSString stringWithFormat:@"%@", name];
}

- (void)updateImageButtonForPerson:(ABRecordRef)person {
	CFDataRef imgData = ABPersonCopyImageData(person);
	UIImage *contactImage = [UIImage imageNamed:@"noImage.png"];
	
	if (imgData != nil) {
		contactImage = [UIImage imageWithData:(NSData *)imgData];
		CFRelease(imgData);
	}
	[editContact setBackgroundImage:contactImage forState:UIControlStateNormal];
}

- (void)updateAddressLabelForPerson:(ABRecordRef)person {
	
	ABMultiValueRef addressValues = ABRecordCopyValue(person, kABPersonAddressProperty);
	NSArray *arrAddresses = [NSArray
							 arrayWithArray:(id)ABMultiValueCopyArrayOfAllValues(addressValues)];
	
	if ([arrAddresses count]) {
		NSDictionary *dictAddress = [arrAddresses objectAtIndex:0];
		NSString *street = [dictAddress objectForKey:(NSString *)kABPersonAddressStreetKey];
		NSString *city = [dictAddress objectForKey:(NSString *)kABPersonAddressCityKey];
		NSString *state = [dictAddress objectForKey:(NSString *)kABPersonAddressStateKey];
		NSString *zip = [dictAddress objectForKey:(NSString *)kABPersonAddressZIPKey];
		NSString *country = [dictAddress objectForKey:(NSString *)kABPersonAddressCountryKey];
		labelAddress.text = [NSString stringWithFormat:@"%@\n%@ %@ %@\n%@",
							 street, city, state, zip, country];
	}
}

- (void)editContact:(id)sender {
	[self presentModalViewController:navEdit animated:YES];
}

- (void)showPeoplePicker:(id)sender {
	[self presentModalViewController:picker animated:YES];
}

- (void)closeEdit:(id)sender {
	
	ABRecordRef person = editContactViewController.displayedPerson;
	[self updateNameLabelForPerson:person];
	[self updateImageButtonForPerson:person];
	[self updateAddressLabelForPerson:person];
	
	[navEdit dismissModalViewControllerAnimated:YES];
}

- (void)createUI {
	showPicker = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[showPicker addTarget:self action:@selector(showPeoplePicker:) forControlEvents:UIControlEventTouchUpInside];
	[showPicker setFrame:CGRectMake(190, 10, 120, 38)];
	[showPicker setTitle:@"People Picker" forState:UIControlStateNormal];
	[self.view addSubview:showPicker];
	
	labelContactName = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 300, 28)];
	labelContactName.backgroundColor = [UIColor clearColor];
	[self.view addSubview:labelContactName];
	
	labelAddress = [[UILabel alloc] initWithFrame:CGRectMake(10, 110, 300, 70)];
	labelAddress.numberOfLines = 3;
	labelAddress.backgroundColor = [UIColor clearColor];
	[self.view addSubview:labelAddress];
	
	editContact = [UIButton buttonWithType:UIButtonTypeCustom];
	[editContact addTarget:self
					action:@selector(editContact:)
		  forControlEvents:UIControlEventTouchUpInside];
	[editContact setFrame:CGRectMake(10,10,60,60)];
	[editContact setEnabled:NO];
	[self.view addSubview:editContact];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	[self createUI];
	
	picker = [[ABPeoplePickerNavigationController alloc] init];
	picker.peoplePickerDelegate = self;
	
	editContactViewController = [[ABPersonViewController alloc] init];
	editContactViewController.allowsEditing = YES;
	UIBarButtonItem *closeButton = [[UIBarButtonItem alloc]
									initWithTitle:@"Close"
									style:UIBarButtonItemStylePlain
									target:self
									action:@selector(closeEdit:)];
	[[editContactViewController navigationItem] setLeftBarButtonItem:closeButton];
	[closeButton release];
	navEdit = [[UINavigationController alloc]
			   initWithRootViewController:editContactViewController];
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
	  shouldContinueAfterSelectingPerson:(ABRecordRef)person {
	
	return YES;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
	  shouldContinueAfterSelectingPerson:(ABRecordRef)person
								property:(ABPropertyID)property
							  identifier:(ABMultiValueIdentifier)identifier {
	
	[self updateNameLabelForPerson:person];
	[self updateImageButtonForPerson:person];
	[self updateAddressLabelForPerson:person];
	
	editContactViewController.displayedPerson = person;
	[editContact setEnabled:YES];
	
	[peoplePicker dismissModalViewControllerAnimated:YES];
	
	return NO;
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
	
	[peoplePicker dismissModalViewControllerAnimated:YES];
}

- (void)dealloc {
	[picker release];
	
	[editContactViewController release];
	[navEdit release];
	[labelContactName release];
	[labelAddress release];
	
    [super dealloc];
}

@end
